/*
 * wit_src.c
 *
 *  Created on: 2024年2月28日
 *      Author: 86176
 */

#include "wit_c_sdk.h"
#include "uart_485.h"
#include "fsl_debug_console.h"
#define ACC_UPDATE		0x01
#define GYRO_UPDATE		0x02
#define ANGLE_UPDATE	0x04
#define MAG_UPDATE		0x08
#define READ_UPDATE		0x80

#define VOL_UPDATE      0x10
#define CUR_UPDATE      0x11
#define TEMP_UPDATE    0x12
#define RED_S_UPDATE    0x13

static volatile char s_cDataUpdate = 0, s_cCmd = 0xff;
const uint32_t c_uiBaud[10] = {0, 4800, 9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600};
static void SensorUartSend(uint8_t *p_data, uint32_t uiSize);
static void CopeSensorData(uint32_t uiReg, uint32_t uiRegNum);

void wit_init()
{
    Uart485_init();
//	WitInit(WIT_PROTOCOL_MODBUS, 0x50);
	WitSerialWriteRegister(SensorUartSend);
	WitRegisterCallBack(CopeSensorData);
//	WitDelayMsRegister(Delayms);

	}

static void SensorUartSend(uint8_t *p_data, uint32_t uiSize)
{
	/*******************************/
	Uart485_send(p_data,uiSize);
}
static void CopeSensorData(uint32_t uiReg, uint32_t uiRegNum)
{
	PRINTF("uiReg:%x\r\n", uiReg);
	switch(uiReg)
	{
		case VOLTAGE:
			s_cDataUpdate = VOL_UPDATE;
		break;
		case CURRENT:
			s_cDataUpdate = CUR_UPDATE;
		break;
		case REDSPEED:
			s_cDataUpdate = RED_S_UPDATE;
		break;
		case W_TEMP:
			s_cDataUpdate = TEMP_UPDATE;
//		break;
////            case AX:
////            case AY:
		case 0x50:
			s_cDataUpdate = 0x50;//加速度：加速度x，加速度y，加速度z
		break;
////            case GX:
////            case GY:
//		case GZ:
//			s_cDataUpdate = GYRO_UPDATE;//角加速度：角速度x，角速度y，角速度z
//		break;
////            case HX:
////            case HY:
//		case HZ:
//			s_cDataUpdate = MAG_UPDATE;
//		break;
////            case Roll:
////            case Pitch:
//		case Yaw:
//			s_cDataUpdate = ANGLE_UPDATE;//角度：滚转角，俯仰角，航向角
//		break;
		default:
			s_cDataUpdate = 0;
		break;
	}
}
//读取焊枪加速度计
uint32_t read_WitA_data()
{
	float fAcc[3], fGyro[3], fAngle[3];
	int i;
	WitInit(WIT_PROTOCOL_MODBUS_A,0x50);
	WitReadReg(AX, 3);

	if(s_cDataUpdate == 0x50)
	{
		PRINTF("s_cDataUpdate:%x\r\n", s_cDataUpdate);
		for(i = 0; i < 3; i++)
		{
			fAcc[i] = sReg[AX+i] / 32768.0f * 16.0f;
			fGyro[i] = sReg[GX+i] / 32768.0f * 2000.0f;
			fAngle[i] = sReg[Roll+i] / 32768.0f * 180.0f;
		}
		PRINTF("acc:%.3f %.3f %.3f\r\n", fAcc[0], fAcc[1], fAcc[2]);
		s_cDataUpdate = 0;
//		if(s_cDataUpdate & ACC_UPDATE)
//		{
//			PRINTF("acc:%.3f %.3f %.3f\r\n", fAcc[0], fAcc[1], fAcc[2]);
//			s_cDataUpdate = 0;
//		}
//		if(s_cDataUpdate & GYRO_UPDATE)
//		{
//			PRINTF("gyro:%.3f %.3f %.3f\r\n", fGyro[0], fGyro[1], fGyro[2]);
//			s_cDataUpdate &= ~GYRO_UPDATE;
//		}
//		if(s_cDataUpdate & ANGLE_UPDATE)
//		{
//			PRINTF("angle:%.3f %.3f %.3f\r\n", fAngle[0], fAngle[1], fAngle[2]);
//			s_cDataUpdate &= ~ANGLE_UPDATE;
//		}
//		if(s_cDataUpdate & MAG_UPDATE)
//		{
//			PRINTF("mag:%d %d %d\r\n", sReg[HX], sReg[HY], sReg[HZ]);
//			s_cDataUpdate &= ~MAG_UPDATE;
//		}
		return (uint32_t)(fAcc[0]*1000);
	}
	else
	{
		s_cDataUpdate = 0;
		return -1;
	}
}
//读取电压
uint32_t read_witV_data()
{
	uint32_t fvol[1];
	WitInit(WIT_PROTOCOL_MODBUS_V,WIT_VOLTAGE_ADDR);
	WitReadReg(VOLTAGE, 1);
	if(s_cDataUpdate == VOL_UPDATE)
	{
		fvol[1] = sReg[VOLTAGE];
		s_cDataUpdate = 0;
		return fvol[1];
	}
	else
	{
		s_cDataUpdate = 0;
		return 0;
	}


}
//读取电流
uint32_t read_witC_data()
{
	uint32_t fcur[1];
	WitInit(WIT_PROTOCOL_MODBUS_C,WIT_CURRENT_ADDR);
	WitReadReg(CURRENT, 1);
	if(s_cDataUpdate == CUR_UPDATE)
	{
		 fcur[1] = sReg[CURRENT];
		 s_cDataUpdate = 0;
		 return fcur[1];
	}
	else
	{
		s_cDataUpdate = 0;
		return 0;
	}

}
//读取温度
uint32_t read_witT_data()
{
	uint32_t ftemp[1];
	WitInit(WIT_PROTOCOL_MODBUS_T,WIT_TEMP_ADDR);
	WitReadReg(W_TEMP, 1);
	if(s_cDataUpdate)
	{
		ftemp[1] = sReg[W_TEMP];
		s_cDataUpdate = 0;
		 return ftemp[1];
	}
	else
	{
		s_cDataUpdate = 0;
		return 0;
	}

}
//读取送丝速度
uint32_t read_witR_data()
{
	uint32_t fred_speed[1];
	WitInit(WIT_PROTOCOL_MODBUS_R,WIT_REDSPEED_ADDR);
	WitReadReg(REDSPEED, 1);
	if(s_cDataUpdate == REDSPEED)
	{
		fred_speed[1] = sReg[REDSPEED];
		s_cDataUpdate = 0;
		return fred_speed[1];
	}
	else
	{
		s_cDataUpdate = 0;
		return 0;
	}

}

